package org.agiletestplus.client.ui;

import org.agiletestplus.client.objects.Project;
import org.damour.base.client.objects.PermissibleObject;
import org.damour.base.client.service.BaseServiceCache;
import org.damour.base.client.ui.IGenericCallback;
import org.damour.base.client.ui.buttons.Button;
import org.damour.base.client.ui.dialogs.MessageDialogBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CreateProjectPanel extends VerticalPanel {
	public CreateProjectPanel(final IGenericCallback<Project> callback, final Project project) {
	    add(new Label("Project Name"));
	    final TextBox nameTextBox = new TextBox();
	    nameTextBox.setText(project.getName());
	    add(nameTextBox);
	    add(new Label("Project Description"));
	    final TextArea descriptionTextArea = new TextArea();
	    descriptionTextArea.setText(project.getDescription());
	    add(descriptionTextArea);
	    HorizontalPanel panel = new HorizontalPanel();
	    final Button createButton = new Button("Create Project");
	    if (project.getId() != null) {
	    	createButton.setText("Edit Project");
	    }
	    panel.add(createButton);
	    
	    final Button cancelButton = new Button("Cancel");
	    panel.add(cancelButton);
    
	    add(panel);
	    
	    cancelButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				callback.invoke(project);
				
			}
	    	
	    });
	    
	    createButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	project.setName(nameTextBox.getText());
	        	project.setDescription(descriptionTextArea.getText());
	        	final AsyncCallback<PermissibleObject> saveCallback = new AsyncCallback<PermissibleObject>() {
	                public void onFailure(Throwable caught) {
	                  MessageDialogBox dialog = new MessageDialogBox("Error", "Could not edit project.", true, true, true);
	                  dialog.center();
	                }

	                public void onSuccess(PermissibleObject savedProject) {
	                  if (savedProject == null) {
	                    MessageDialogBox dialog = new MessageDialogBox("Error", "Could not edit project.", true, true, true);
	                    dialog.center();
	                  } else {
	                	 if (project.getId() != null) {
	                		 MessageDialogBox dialog = new MessageDialogBox("Success", "Project Updated.", true, true, true);
	                		 dialog.center();
	                	 } else {
	                		 MessageDialogBox dialog = new MessageDialogBox("Success", "Project Created.", true, true, true);
	                		 dialog.center();
	                		 
	                	 }
	                    callback.invoke((Project)savedProject);
	                  }
	                };
	              };

	              BaseServiceCache.getService().savePermissibleObject(project, saveCallback);
	        }
	    });
	}
}
